package bt.basics.common.utils;

import bt.basics.common.exception.CustomizeException;
import bt.basics.common.exception.ErrorCode;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

/**
 * 本地文件上传工具类
 */
public class LocalhostFileUtil {

    public static String upload(byte[] data,String localPath, String path) {
        return upload(new ByteArrayInputStream(data),localPath, path);
    }

    public static String upload(InputStream inputStream,String localPath,String path) {
        File file = new File(localPath + File.separator + path);
        try {
            FileUtils.copyToFile(inputStream, file);
        } catch (IOException e) {
            throw new CustomizeException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e, "");
        }
        return  "/" + path;
    }

    /**
     *
     * @param data 文件base64
     * @param model  模块
     * @param path  文件路径
     * @param suffix  文件后缀
     * @return
     */
    public static String uploadSuffix(byte[] data,String model,String path, String suffix) {
        return upload(data,path, getPath(model, suffix));
    }

    /**
     *
     * @param inputStream 文件流
     * @param model  模块
     * @param path  文件存储路径
     * @param suffix  文件后缀
     * @return
     */
    public static String uploadSuffix(InputStream inputStream,String model,String path, String suffix) {
        return upload(inputStream,path, getPath(model, suffix));
    }

    /**
     * 文件路径
     * @param prefix 前缀
     * @param suffix 后缀
     * @return 返回上传路径
     */
    public static String getPath(String prefix, String suffix) {
        //生成uuid
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //文件路径
        String path = DateUtils.format(new Date(), "yyyyMMdd") + "/" + uuid;

        if(StringUtils.isNotBlank(prefix)){
            path = prefix + "/" + path;
        }

        return path + "." + suffix;
    }

    public static boolean checkFileTyoeSize(Long len,String type){
        boolean falg = true;
        if(StringUtils.isEmpty(type)){
            type = "default";
        }
        //默认
        if(type.equals("default")){
            falg = checkFileSize(len,10,"M");
        }
        //图片
        if(type.equals("image")){
            falg = checkFileSize(len,5,"M");
        }
        //视频
        if(type.equals("video")){
            falg = checkFileSize(len,200,"M");
        }
        //文档
        if(type.equals("document")){
            falg = checkFileSize(len,50,"M");
        }
        //压缩文件
        if("compression".equals(type)){
            falg = checkFileSize(len, 500,"M");
        }
        return falg;
    }

    /**
     * 判断文件大小
     *
     * @param len
     *            文件长度
     * @param size
     *            限制大小
     * @param unit
     *            限制单位（B,K,M,G）
     * @return
     */
    public static boolean checkFileSize(Long len, int size, String unit) {
        double fileSize = 0;
        if ("B".equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if ("K".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if ("M".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if ("G".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }
}
